#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
vector<int> adj[N], sccadj[N];
int dfn[N], low[N], deg[N], sz[N], scc[N], memo[N], dfncnt, scccnt, ans;
bool instk[N];
int n, m;
stack<int> s;

void tarjan(int u) {
  dfn[u] = low[u] = ++dfncnt;
  s.push(u);
  instk[u] = true;
  for (auto v : adj[u]) {
    if (!dfn[v]) {
      tarjan(v);
      low[u] = min(low[u], low[v]);
    } else if (instk[v]) {
      low[u] = min(low[u], dfn[v]);
    }
  }
  if (dfn[u] == low[u]) {
    scccnt++;
    int k = -1;
    do {
      k = s.top();
      s.pop();
      sz[scccnt]++;
      instk[k] = false;
      scc[k] = scccnt;
    } while (k != u);
  }
}

int dfs(int u) {
  if (memo[u]) return memo[u];
  for (const int v : sccadj[u]) memo[u] = max(memo[u], dfs(v));
  return memo[u] = memo[u] + sz[u];
}

int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    cin >> n >> m;

    for (int *arr : {dfn, low, deg, sz, scc, memo}) {
      memset(arr, 0, sizeof(dfn));
    }
    memset(instk, 0, sizeof instk);
    while (s.size()) s.pop();
    dfncnt = scccnt = ans = 0;
    for (auto &v : {adj, sccadj})
      for (int i = 1; i <= n; ++i) v[i].clear();

    for (int i = 1; i <= m; ++i) {
      int u, v;
      cin >> u >> v;
      adj[u].push_back(v);
    }
    for (int i = 1; i <= n; ++i)
      if (!dfn[i]) tarjan(i);

    for (int u = 1; u <= n; ++u) {
      for (int v : adj[u]) {
        if (scc[u] != scc[v]) {
          sccadj[scc[u]].push_back(scc[v]);
        }
      }
    }
    for (int i = 1; i <= scccnt; ++i) {
      ans = max(ans, dfs(i));
    }
    cout << ans << endl;
  }
}
